package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

public class PactDetailsActivity extends BaseActivity implements View.OnClickListener {

    String url;

    private boolean isFlag = false;
    private final static int CODE_COMPACT = 946;
    private String msg;

    public static Intent createIntent(Context context, String url) {
        Intent intent = new Intent(context, PactDetailsActivity.class);
        intent.putExtra("url", url);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
    }

    private WebView mWebView;
    private ImageView iv_choose_img;
    private String data = "0";
    private boolean isTrue = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pact_details);
        parseIntent();
        initTitle();
        initView();
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("标准合同");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        iv_choose_img = (ImageView) findViewById(R.id.iv_choose_img);
        iv_choose_img.setOnClickListener(this);
        findViewById(R.id.tv_submit).setOnClickListener(this);
        findViewById(R.id.tv_standard).setOnClickListener(this);

    }

    private void initView() {
        mWebView = (WebView) findViewById(R.id.webView);
        Log.e("==是网页的=", " 进来了");
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        mWebView.loadUrl(url);
    }


    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //选择
            case R.id.iv_choose_img:
                isTrue = !isTrue;
                if (isTrue) {
                    iv_choose_img.setImageResource(R.mipmap.ic_choose_nor);
                    isTrue = true;
                    data = "0";
                } else {
                    iv_choose_img.setImageResource(R.mipmap.ic_choose_li);
                    data = "1";
                }
                break;
            //提交
            case R.id.tv_submit:
                if ("0".equals(data)) {
                    MyToast("请选择同意合同内容,如不同意请选择自定义合同");
                } else {
                    intent = new Intent(this, ProjectOrderActivity.class);
                    intent.putExtra("pact", "1");
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;

            //选择自定义合同
            case R.id.tv_standard:
                intent = new Intent(this, UserDefinedCompactActivity.class);
                intent.putExtra("msg", msg);
                startActivityForResult(intent, CODE_COMPACT);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CODE_COMPACT:
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        String pact = data.getStringExtra("pact");
                        if ("0".equals(pact)) {
                            Intent intent = new Intent(this, ProjectOrderActivity.class);
                            intent.putExtra("pact", "0");
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    }
                }
                break;

            default:
                break;
        }
    }
}